package com.skjanyou.applicationcase.util;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import com.skjanyou.applicationcase.bean.Shortcut;
import com.skjanyou.applicationcase.windows.WindowsShortcut;
import com.skjanyou.desktop.window.Window;
import com.skjanyou.util.CommUtil;
import com.skjanyou.util.FileUtil;

public class WindowsUtil {

	public static Dimension getScreenSize(){
		return Toolkit.getDefaultToolkit().getScreenSize();
	}
	
	public static List<Shortcut> getAllShortCut(){
		List<Shortcut> result = new ArrayList<>();
		String desktopPth = desktopPath();
		File desktopFile = new File(desktopPth);
		File[] lnkFiles = desktopFile.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File arg0, String name) {
				return name.endsWith(".lnk");
			}
		});
		
		for (File file : lnkFiles) {
			String realFileName = shortcutLnk(file);
			if( realFileName != null ){
				File realFile = new File(realFileName);
				Shortcut sc = new Shortcut();
				sc.setTitle(realFile.getName().substring(0, realFile.getName().lastIndexOf(".")));
				sc.setOnClick("Skjanyou.openShortcut('" + realFile.getAbsolutePath().replace("\\", "/")  + "')");
				sc.setImg("./img/icon/doc.png");
				result.add(sc);
			}
		}
		
		return result;
	}
	
	public static void tray( Window window,String iconImage ){
		InputStream is = null;
		try{
			if( iconImage.startsWith("classpath:") ){
				is = Thread.currentThread().getContextClassLoader().getResourceAsStream(iconImage.substring(10));
			}else{
				is = new FileInputStream(iconImage);
			}

			Image image = ImageIO.read(is);
			if(SystemTray.isSupported()){
				SystemTray st = SystemTray.getSystemTray();
				TrayIcon ti = new TrayIcon(image);
				try {
					st.add(ti);
				} catch (AWTException e1) {
					e1.printStackTrace();
				}
				PopupMenu jm = new PopupMenu();
				MenuItem exit = new MenuItem("退出程序");
				MenuItem show = new MenuItem("显示窗口");
				MenuItem hide = new MenuItem("隐藏窗口");
				exit.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						System.exit(0);
					}
				});
				show.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						window.hideWindow();
					}
				});
				hide.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						window.showWindow();
					}
				});
				jm.add(show);jm.add(hide);jm.add(exit);
				ti.setPopupMenu(jm);
			}
		} catch( Exception e ){
			
		} finally {
			CommUtil.close(is);
		}		
	}

	public static String desktopPath(){
		return System.getProperty("user.home") + "\\Desktop";
	}

	public static String shortcutLnk( File lnkFile ){
		String realFile = null;
		try {
			WindowsShortcut ws = new WindowsShortcut(lnkFile);
			realFile = ws.getRealFilename();
		} catch (IOException | ParseException e) {
			//e.printStackTrace();
		}
		return realFile;
	}

	public static boolean executeExeableFile( String exefile,String... argus ){
		boolean result = true;
		try {
			Runtime.getRuntime().exec(exefile, argus);
		} catch (IOException e) {
			e.printStackTrace();
			result = false;
		}

		return result;
	}

	public static boolean executeExeableFileByCopyNewFile( String exefile,String... argus ){
		File f = new File(exefile);
		File newF = new File(f.getParentFile(),"new-" + f.getName());
		FileUtil.copy(f, newF);
		return executeExeableFile(newF.getAbsolutePath(), argus);
	}
}
